package com.common.www.controller;

import com.common.www.bean.entity.Result;
import com.common.www.config.WebConfig;
import com.common.www.service.redis.RedisTemplateService;
import com.common.www.utils.GlobalSessionUtils;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpSession;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@CrossOrigin()
@Api(tags = "API session管理接口")
@RequestMapping(value = {"/mgt/session/"})
public class SessionController {

    @Autowired
    RedisTemplateService redisTemplateService;

    @ApiOperation("查询登录前访问的url")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/getLastUrl", method = RequestMethod.POST)
    public Result getLastUrl() {
        HttpSession session = GlobalSessionUtils.getSession();
        Object o = session.getAttribute(WebConfig.LastUrl);
        if (o == null) {
            return R.ok("/frame/index.html", "请求成功");
        } else {
//            GlobalSessionUtils.getSession().setAttribute(WebConfig.LastUrl, null);
            return R.ok(o);
        }
    }

    @ApiOperation("redis存储测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "String", defaultValue = ""),
            @ApiImplicitParam(name = "value", value = "String", defaultValue = ""),
            @ApiImplicitParam(name = "time", value = "秒为单位", defaultValue = ""),
    })
    @RequestMapping(value = "/redisSaveTest", method = RequestMethod.POST)
    public Result redisSaveTest(String key, String value, Long time) {
        if (ParamsUtils.isInputEmpty(key, value)) {
            return R.fail("参数缺失");
        }
        if (time == null) {
            redisTemplateService.set(key, value);
        } else {
            redisTemplateService.set(key, value, time, TimeUnit.SECONDS);
        }
        return R.ok();
    }

    @ApiOperation("redis查询测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/redisGetTest", method = RequestMethod.POST)
    public Result redisGetTest(String key) {
        if (ParamsUtils.isInputEmpty(key)) {
            return R.fail("参数缺失");
        }
        String res = redisTemplateService.get(key);
        return R.ok(res);
    }

    @ApiOperation("根据id获取session")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/getSessionById", method = RequestMethod.POST)
    public Result getSessionById(String id) {
        if (ParamsUtils.isInputEmpty(id)) {
            return R.fail("参数缺失");
        }
        HttpSession session = GlobalSessionUtils.getSession().getSessionContext().getSession(id);
        return R.ok(session);
    }

    @ApiOperation("根据id获取session")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/deleteSessionByKey", method = RequestMethod.POST)
    public Result deleteSessionByKey(String key) {
        if (ParamsUtils.isInputEmpty(key)) {
            return R.fail("参数缺失");
        }
        redisTemplateService.delete(key);
        return R.ok();
    }
}
